library(haven)
library(foreign)
library(readxl)
library(openxlsx)
library(car)
library(dplyr)
library(readstata13)

# Import data
d <- read_excel("Data for Affluence and influence in a social democracy.xlsx")

######################################################################
#---------------------------- START ---------------------------------# 
######################################################################

# Make percentage Dont Know
d$pcFAV_EDU1 <- d$EDU1_FAV/(d$EDU1_FAV+d$EDU1_OPP)
d$pcFAV_EDU2 <- d$EDU2_FAV/(d$EDU2_FAV+d$EDU2_OPP)
d$pcFAV_EDU3 <- d$EDU3_FAV/(d$EDU3_FAV+d$EDU3_OPP)
d$pcFAV_EDU4 <- d$EDU4_FAV/(d$EDU4_FAV+d$EDU4_OPP)
d$pcFAV_EDU5 <- d$EDU5_FAV/(d$EDU5_FAV+d$EDU5_OPP)
d$pcFAV_EDU6 <- d$EDU6_FAV/(d$EDU6_FAV+d$EDU6_OPP)
d$pcFAV_EDU7 <- d$EDU7_FAV/(d$EDU7_FAV+d$EDU7_OPP)
d$pcFAV_EDU8 <- d$EDU8_FAV/(d$EDU8_FAV+d$EDU8_OPP)
d$pcFAV_EDU9 <- d$EDU9_FAV/(d$EDU9_FAV+d$EDU9_OPP)
d$pcFAV_EDU10 <- d$EDU10_FAV/(d$EDU10_FAV+d$EDU10_OPP)

# Set N for each income group to N who responded
d$S_EDU1 <- (d$EDU1_FAV+d$EDU1_OPP)
d$S_EDU2 <- (d$EDU2_FAV+d$EDU2_OPP)
d$S_EDU3 <- (d$EDU3_FAV+d$EDU3_OPP)
d$S_EDU4 <- (d$EDU4_FAV+d$EDU4_OPP)
d$S_EDU5 <- (d$EDU5_FAV+d$EDU5_OPP)
d$S_EDU6 <- (d$EDU6_FAV+d$EDU6_OPP)
d$S_EDU7 <- (d$EDU7_FAV+d$EDU7_OPP)
d$S_EDU8 <- (d$EDU8_FAV+d$EDU8_OPP)
d$S_EDU9 <- (d$EDU9_FAV+d$EDU9_OPP)
d$S_EDU10 <- (d$EDU10_FAV+d$EDU10_OPP)

# Make prediction varaibles
d$edupred90 <- NA
d$edupred70 <- NA
d$edupred50 <- NA
d$edupred30 <- NA
d$edupred10 <- NA

# For-loop that for each row finds the percentile midpoint for each category 
# in that survey's education variable, then uses those scores as IV and the group's 
# preference (% support) as DV in a  logistic regression model weigted by the 
# n of each group. Then uses resulting coefficients to impute preferences of 
# desired percentiles (e.g. 90th, 70th, 50th, etc.).   
for(i in 1:nrow(d)){
  drow <- d[i,]
  d1 <- drow %>% select(starts_with("S_EDU"))
  d1 <- as.data.frame(t(d1))
  d1$prop <- d1$V1/sum(d1$V1, na.rm=T)
  d1$cumu <- cumsum(d1$prop)
  d1 <- mutate(d1, score = ((cumu - lag(cumu))/2)+lag(cumu)) # Make percentile midpoint scores
  d1$score[1] <- d1$cumu[1]-(d1$cumu[1]/2) # Make percentile midpoint score for first category since formula above returns NA for that one
  d1$edu <- d1$Var1
  d1 <- add_rownames(d1, "edu")
  names(d1)[names(d1)=="V1"] <- "n"
  d1 <- select(d1, edu, score, n)
  d2 <- drow %>% select(starts_with("pcFAV_EDU"))
  d2 <- as.data.frame(t(d2)) 
  d2$edu <- 1:nrow(d2) 
  names(d2)[names(d2)=="V1"] <- "sup"
  d3 <- merge(d1,d2, by="edu")
  m1 <- glm(sup ~ score + I(score^2), data=d3, weights=n, family = "binomial") # Education and education^2 as IVs, just like Gilens (2012, 61)
  d4 <- data.frame(score=c(0.90, 0.70, 0.50, 0.30, 0.10))
  d4$pred <- predict(m1, d4, type="response")
  d$edupred90[i] <- d4$pred[1]
  d$edupred70[i] <- d4$pred[2]
  d$edupred50[i] <- d4$pred[3]
  d$edupred30[i] <- d4$pred[4]
  d$edupred10[i] <- d4$pred[5]
}

write.xlsx(d, "Data for Affluence and influence in a social democracy.xlsx")

######################################################################
#----------------------------- END ----------------------------------# 
######################################################################


